package com.shortlink.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shortlink.admin.dao.entity.ShortLinkDO;
import com.shortlink.admin.dto.req.recyclebin.RecycleBinPageReqDTO;
import com.shortlink.admin.dto.resp.shortlink.ShortLinkVO;
import com.shortlink.admin.remote.ShortLinkMapperRemoteService;
import com.shortlink.admin.service.RecycleBinService;
import com.shortlink.admin.service.ShortLinkService;
import com.shortlink.common.base.Result;
import com.shortlink.common.exception.ClientException;
import com.shortlink.common.exception.RemoteException;
import com.shortlink.common.exception.ServiceException;
import com.shortlink.common.user.UserContext;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 回收站接口实现层
 *
 * @author wp
 */
@Service
@RequiredArgsConstructor
public class RecycleBinServiceImpl implements RecycleBinService {
    private final ShortLinkService shortLinkService;
    private final ShortLinkMapperRemoteService shortLinkMapperRemoteService;

    @Override
    public IPage<ShortLinkVO> page(RecycleBinPageReqDTO recycleBinPage) {
        Page<ShortLinkDO> page = new Page<>(recycleBinPage.getCurrent(), recycleBinPage.getSize());
        QueryWrapper<ShortLinkDO> queryWrapper = new QueryWrapper<ShortLinkDO>()
                .eq(ShortLinkDO.USER_ID_NAME, UserContext.getUserId())
                .eq(ShortLinkDO.DEL_FLAG_NAME, ShortLinkDO.DEL_FLAG_SUCCESS);

        return shortLinkService.queryPage(page, queryWrapper, recycleBinPage.getOrderTag(), recycleBinPage.getDesc());
    }

    @Transactional
    @Override
    public void recover(Long id) {
        boolean succeed = shortLinkService.lambdaUpdate()
                .eq(ShortLinkDO::getId, id)
                .eq(ShortLinkDO::getUserId, UserContext.getUserId())
                .eq(ShortLinkDO::getDelFlag, ShortLinkDO.DEL_FLAG_SUCCESS)
                .set(ShortLinkDO::getDelFlag, ShortLinkDO.DEL_FLAG_FAIL)
                .update();
        if (!succeed) {
            throw new ServiceException("短链接恢复失败");
        }

        Result<Void> result = shortLinkMapperRemoteService.recover(id);
        succeed = result.isSuccess();
        if (!succeed) {
            throw new RemoteException(result.getMessage());
        }
    }

    @Override
    public void delete(Long id) {
        boolean succeed = shortLinkService.lambdaUpdate()
                .eq(ShortLinkDO::getId, id)
                .eq(ShortLinkDO::getUserId, UserContext.getUserId())
                .eq(ShortLinkDO::getDelFlag, ShortLinkDO.DEL_FLAG_SUCCESS)
                .remove();
        if (!succeed) {
            throw new ClientException("用户回收站短链接不存在");
        }
        Result<Void> result = shortLinkMapperRemoteService.recover(id);
        succeed = result.isSuccess();
        if (!succeed) {
            throw new RemoteException(result.getMessage());
        }
    }
}
